import concurrent.futures
import json
import os

import pandas as pd
import requests

# 获取当前 Python 文件的路径
current_dir = os.path.dirname(os.path.abspath(__file__))

# 指定存放 JSON 文件的文件夹路径
folder_path = os.path.join(current_dir, 'resource', 'detail')

# 创建一个空的 DataFrame 用于存储数据
dfs = []

# 自定义请求头
headers = {
    'accept': 'application/json, text/javascript, */*; q=0.01',
    'request-from': '1',
    'Origin': 'https://erp.fangline.cn',
    'x-requested-with': 'XMLHttpRequest',
    'page-name': '/visit-house/house-list',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36',
    'cookie': 'JSESSIONID=84bcdb7a-7d48-42b7-b76e-e913810df702; guideStatus=6; SERVERID=f595ac9d48f8f7494ff45c92e36e47bc|1707043354|1707043351',
    'Content-Type': 'application/json',
    'Cache-Control': 'no-cache',
    'Postman-Token': '43d3c843-d672-4acd-b360-8454017874db',
    'Host': 'erp.fangline.cn',
    'Accept-Encoding': 'gzip, deflate, br',
    'Connection': 'keep-alive'
}

# 读取文件夹中的文件名
file_names = os.listdir(folder_path)

# 对文件名进行排序
sorted_file_names = sorted(file_names)

# 读取record.txt文件中的数字
with open(os.path.join(current_dir, 'resource', 'record.txt'), 'r') as record_file:
    target_number = int(record_file.read())


for filename in sorted_file_names:
    """循环读取文件夹中的 JSON 文件
    """
    if filename.endswith('.json'):
        # 提取文件名中的数字部分
        file_number = int(filename.split('.')[0])
        
        # 比较文件名中的数字与目标数字
        if file_number <= target_number:
            continue  # 跳过本次循环，执行下一个循环
    
        file_path = os.path.join(folder_path, filename)

        # 读取文件中的 JSON 数据，指定编码方式为 'utf-8'
        with open(file_path, 'r', encoding='utf-8') as file:
            json_data = json.load(file)

        # 房东信息
        house_id = json_data['house']['houseId']
        url = f'http://erp.fangline.cn/house/list-house-owner.json?houseId={house_id}'
        response = requests.post(url, headers=headers)
        if response.status_code == 316:
            print('获取owner用户信息失败!')
            break
        owners = response.json()
        owner = owners['ownerList'][0]
        house = json_data['house']

        # 图片信息
        # url = f'http://erp.fangline.cn/house/getHouseImgList.json?houseId={house_id}'
        # response = requests.post(url, headers=headers)
        # images = response.json()['indoorImg']
        # imageUrls = []
        # for image in images:
        #     imageUrls.append(image['url'])

        # 跟进信息
        url = f'http://erp.fangline.cn/house/get-recently-house-log.json?houseId={house_id}'
        response = requests.post(url, headers=headers)
        follows = response.json()['followLogs']
        followsStr = ''
        for item in follows:
            # D1小胡 13650398398 广东省广州市番禺区 洛浦街南浦西二华信彩印有限公司西北门旁桥底1号仓
            followsStr += f"{item['userName']} 在 {item['createTimeString']}, 评分 {item['stars']} 星，状态：{item['logType']},评价：{item['content']} \n"

        # 提取需要的属性
        extracted_data = {
            '房源id': house_id,  # 房源id
            '小区': house['section'],  # 梅园小区
            '栋': house['blockNo'],   # 1
            '单元': house['unitNo'],   # 1
            '室': house['cell'],  # 1703 室
            '总价（万）': house['price'],  #
            '元/平': house['unitPrice'],  # 5060 元/平
            '面积': house['area'],  # 88 平
            '室': house['room'],  # 室
            '厅': house['hall'],  # 厅
            '卫': house['toilet'],  # 卫
            '楼层': house['floor'],  # 17 层
            '总层': house['totalFloor'],  # 26 层
            '装修风格': house['decoration'],  # 精装
            '房东': owner['ownerName'],  # 李伟
            '房东电话': owner['tel'],  # 18156815991
            '房图位置': f'图片在{house_id}文件夹',  # 图片集合
            '跟进记录': followsStr
        }

        # # 将提取的数据转换为 DataFrame
        extracted_df = pd.DataFrame([extracted_data])

        # 将当前文件的数据存储到列表中
        dfs.append(extracted_df)

# 将所有 DataFrame 连接在一起
df = pd.concat(dfs, ignore_index=True)

# 指定输出的 Excel 文件路径
output_file = os.path.join(folder_path, '房源汇总数据.xlsx')

# 将 DataFrame 写入 Excel 文件
df.to_excel(output_file, index=False)

# 打印输出文件路径
print(f'Saved data to Excel file: {output_file}')
